<?php
/**
 * Created by PhpStorm.
 * User: 70767
 * Date: 2020/6/7
 * Time: 10:24
 */

namespace app\admin\controller;

use think\Db;
use think\Validate;

class Indexpage extends Base
{
    /**
     * 显示
     */
    public function index(){
        $db = Db::name('conf')->where(['id'=>1])->find();
        if($db) {
            if ($db['banner']) {
                $db['banner'] = explode(',', $db['banner']);
            } else {
                $db['banner'] = [];
            }
        }

        $this->returnMsg('1', '', $db);
    }

    /**
     * 更改
     */
    public function update()
    {
        $get_data = $this->body_data;
        $banner_data = $get_data['banner'];
        $about = $get_data['about'];
        $exoneration = $get_data['exoneration'];

        $banner = "";
        if($banner_data) {
            $banner = implode(',', $banner_data);
        }

        $update_db = ['banner'=>$banner,'about'=>$about,'exoneration'=>$exoneration];

        $rule = [
            'about' => 'max:1000',
            'exoneration' => 'max:1000',
        ];

        $msg = [
            'about.max' => '请输入仓库名字',
            'exoneration.max' => '请输入1-50字的仓库名字',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($update_db);
        if (!$result) {
            return ['status' => '-1', 'msg' => $validate->getError()];
        }


        Db::name('conf')->where(['id'=>1])->update($update_db);

        $this->returnMsg('1', '修改成功');
    }
}